package com.cndw.kungfu.model.pvp;

import java.util.Map;

import org.jboss.netty.util.internal.ConcurrentHashMap;

import com.cndw.kungfu.ext.TimeProcesser;


/**
 * <p> Title: 在线经验灵气抢夺 </p>
 * 
 * @author Skycat
 * @version 1.0 2012-6-8
 */
public class OnlineExpAnimaWrest
{
	/**
	 * 灵气位置及加成
	 */
	public static int[] onlineWrestIdx = {0, 50, 35, 20};	// 加成(绿野仙踪,天降祥瑞,紫气东来)
	/**
	 * 绿野仙踪
	 */
	public static int animaTypeOne = 1;
	/**
	 * 天降祥瑞
	 */
	public static int animaTypeTwo = 2;
	/**
	 * 紫气东来
	 */
	public static int animaTypeThree = 3;
	
	private static int initTime = 0;	// 列表初始化时间
	private static Map<Integer, Long> animaList = new ConcurrentHashMap<Integer, Long>(50);
	
	public static Map<Integer, Long> getAnimaList() {
		int nowTime = TimeProcesser.getUnixTime();
		if (nowTime - initTime > 7200) {	// 超过2小时移除所有位置玩家(每天14:00~16:00可抢夺灵气)
			synchronized (OnlineExpAnimaWrest.class) {
				nowTime = TimeProcesser.getUnixTime();
				if (nowTime - initTime > 7200) {
					for (int i=1; i<=50; i++) {
						animaList.put(i, -1L);
						initTime = nowTime;
					}
				}
			}
		}
		
		return animaList;
	}
	
	/**
	 * 添加
	 * 
	 * @param index
	 * @param playerId
	 * @return boolean
	 */
	public static boolean add(int index, long playerId) {
		if (index < 1 || index > 50) {
			return false;
		}
		
		getAnimaList().put(index, playerId);
		return true;
	}
	
	/**
	 * 移除
	 * 
	 * @param playerId
	 * @return boolean
	 */
	public static boolean remove(int index) {
		if (index < 1 || index > 50) {
			return false;
		}
		
		getAnimaList().put(index, -1L);
		return false;
	}
	
	/**
	 * 获取指定位置上的玩家
	 * @param index
	 * @return long
	 */
	public static long getPlayerIdByIndex(int index) {
		if (index < 1 || index > 50) {
			return -2;
		}
		return getAnimaList().get(index);
	}
	
	/**
	 * 获取灵气类型
	 * 
	 * @param index
	 * @return int
	 */
	public static int getAnimaTypeByIndex(int index) {
		if (index < 1 || index > 50) {
			return -1;
		}
		if (index >= 1 && index <= 5) {
			return 1;
		} else if (index >= 6 && index <= 20) {
			return 2;
		} else {
			return 3;
		}
	}
	
	/**
	 * 根据index获取前端index
	 * @param index
	 * @return int
	 */
	public static int getClientIndex(int index) {
		if (index < 1 || index > 50) {
			return -1;
		}
		if (index <= 5) {
			return index;
		} else if (index > 5 && index <= 20) {
			return index - 5;
		} else {
			return index - 20;
		}
	}
	
	/**
	 * 获取加成比率
	 * @param playerId
	 * @return int
	 */
	public static int getAddRate(long playerId) {
		// 时间限制
		int currentTime = TimeProcesser.getUnixTime();		// 当前时间
		int begin = TimeProcesser.getHourTime("14:00");		// 灵气抢夺开始时间
		int end = TimeProcesser.getHourTime("16:00");		// 灵气抢夺结束时间
		if (currentTime < begin || currentTime > end) {
			return 0;
		}
		for (int i=1; i<=50; i++) {
			if (getAnimaList().get(i) == playerId) {
				if (i <= 5) {
					return onlineWrestIdx[1];
				} else if (i > 5 && i <=20) {
					return onlineWrestIdx[2];
				} else {
					return onlineWrestIdx[3];
				}
			}
		}
		return 0;
	}
	
	/**
	 * 根据灵气加成类型获取加成比率
	 * @param type
	 * @return int
	 */
	public static int getAddRate(int type) {
		if (type < 1 || type > 3) {
			return 0;
		}
		if (1 == type) {
			return 50;
		} else if (2 == type) {
			return 35;
		} else {
			return 20;
		}
	}
	
	/**
	 * 是否灵气抢夺中
	 * @param playerId
	 * @return int
	 */
	public static int isAnimalStatus(long playerId) {
		for (int i=1; i<=50; i++) {
			if (getAnimaList().get(i) == playerId) {
				return i;
			}
		}
		
		return -1;
	}
}